from rest_framework import status
from rest_framework.response import Response


def response_error(msg, errcode=None):
    code = status.HTTP_400_BAD_REQUEST
    if errcode:
        code = errcode
    return Response({"status": False, "message": msg}, status=code)


def response_ok(data=None):
    if data:
        return Response({"status": True, 'data': data})
    else:
        return Response({"status": True})


def get_remote_addr(request):
    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
    if x_forwarded_for:
        ip = x_forwarded_for.split(',')[0]
    else:
        ip = request.META.get('REMOTE_ADDR')
    return ip
